package servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.PrisTeam1Car;

import manager.CarManager;

/**
 * Servlet implementation class SearchCarServlet
 */
@WebServlet("/SearchCarServlet")
public class SearchCarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SearchCarServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String brand = request.getParameter("brand");
		String model = request.getParameter("model");

		String fromPr = request.getParameter("fromPrice");
		int fromPrice;
		if (!fromPr.isEmpty())
			fromPrice = Integer.parseInt(fromPr);
		else
			fromPrice = Integer.MIN_VALUE;
		String toPr = request.getParameter("toPrice");
		int toPrice;
		if (!toPr.isEmpty())
			toPrice = Integer.parseInt(toPr);
		else
			toPrice = Integer.MAX_VALUE;

		String fuelType = request.getParameter("fuelType");

		String fromY = request.getParameter("fromYear");
		int fromYear;
		if (!fromY.isEmpty())
			fromYear = Integer.parseInt(fromY);
		else
			fromYear = Integer.MIN_VALUE;
		String toY = request.getParameter("toYear");
		int toYear;
		if (!toY.isEmpty())
			toYear = Integer.parseInt(toY);
		else
			toYear = Integer.MAX_VALUE;

		String fromP = request.getParameter("fromPower");
		int fromPower;
		if (!fromP.isEmpty())
			fromPower = Integer.parseInt(fromP);
		else
			fromPower = Integer.MIN_VALUE;
		String toP = request.getParameter("toPower");
		int toPower;
		if (!toP.isEmpty())
			toPower = Integer.parseInt(toP);
		else
			toPower = Integer.MAX_VALUE;

		String fromK = request.getParameter("fromKilometers");
		int fromKilometers;
		if (!fromK.isEmpty())
			fromKilometers = Integer.parseInt(fromK);
		else
			fromKilometers = Integer.MIN_VALUE;
		String toK = request.getParameter("toKilometers");
		int toKilometers;
		if (!toK.isEmpty())
			toKilometers = Integer.parseInt(toK);
		else
			toKilometers = Integer.MAX_VALUE;

		CarManager cm = new CarManager();

		List<PrisTeam1Car> cars = cm.searchCars(brand, model, fromPrice,
				toPrice, fuelType, fromYear, toYear, fromPower, toPower,
				fromKilometers, toKilometers);

		request.getSession().setAttribute("cars", cars);

		response.sendRedirect("web/ads.jsp");
	}

}
